package com.qijian.maindata.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.qijian.common.utils.StringUtils;
import com.qijian.maindata.domain.ExchangeRate;
import com.qijian.maindata.mapper.ExchangeRateMapper;
import com.qijian.maindata.service.IExchangeRateService;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;

/**
 * 汇率Service业务层处理
 *
 * @author qijian
 * @date 2022-03-04
 */
@Service
@RequiredArgsConstructor
public class ExchangeRateServiceImpl extends ServiceImpl<ExchangeRateMapper, ExchangeRate> implements IExchangeRateService {

    private final ExchangeRateMapper exchangeRateMapper;

    @Override
    public BigDecimal getLcByCurrencyType(String currencyType, BigDecimal amount) {
        if (amount == null || amount.compareTo(BigDecimal.ZERO) == 0)
            return BigDecimal.ZERO;
        if (StringUtils.isBlank(currencyType))
            return amount;
        QueryWrapper<ExchangeRate> wrapper = new QueryWrapper<>();
        wrapper.eq("currency_type", currencyType);
        wrapper.orderByDesc("`month`");
        wrapper.last("limit 1");
        ExchangeRate exchangeRate = exchangeRateMapper.selectOne(wrapper);
        if (exchangeRate == null || exchangeRate.getExchangeRate() == null || exchangeRate.getExchangeRate().compareTo(BigDecimal.ZERO) == 0)
            return amount;
        return exchangeRate.getExchangeRate().multiply(amount);
    }
}
